﻿package org.dm.3d.Actions.object 
{
	import org.dm.3d.Actions.Action;
	import org.dm.3d.geom.Num3D;
	import org.dm.3d.geom.MathExtend;
	/**
	 * ...
	 * @author Scott
	 */
	public class China extends Action
	{
		private var goalPos:Vector.<Num3D>;
		private var goalNum:uint;
		private var _init:Boolean = false;
		private var _pre:Boolean = true;
		private var _root:Boolean = false;
		private var _cur:uint = 0;
		private var _speed:uint = 20;
		
		public function China() 
		{
			this.isUsed = true;
			goalPos = new Vector.<Num3D>;
			var points:Vector.<int> = Vector.<int>([-300,-67.6,-296.4,-84.4,-292.8,-56.8,-290.4,-44.8,-283.2,-90.4,-280.8,-37.6,-272.4,-89.2,-268.8,-30.4,-262.8,-96.4,-258,-25.6,-250.8,-102.4,-249.6,20,-248.4,8,-247.2,-1.6,-246,-31.6,-246,-13.6,-241.2,29.5,-240,-108.4,-236.4,-146.8,-234,-122.8,-232.8,-134.8,-232.8,-25.6,-229.2,-151.6,-229.2,36.8,-217.2,-151.6,-216,-25.6,-214.8,-158.8,-214.8,39.1,-211.2,-172,-204,-23.1,-202.8,47.5,-195.6,-178,-194.4,53.5,-192,-26.8,-183.6,-179.2,-182.4,-192.4,-182.4,59.5,-178.8,-30.4,-174,-200.8,-166.8,-209.2,-166.8,-35.2,-166.8,64.3,-159.6,-204.4,-153.6,-36.4,-153.6,62,-150,-193.6,-142.8,62,-141.6,-18.4,-141.6,-6.4,-140.4,-31.6,-135.6,-62.8,-134.4,-187.6,-132,-52,-132,62,-130.8,2,-129.6,-176.8,-129.6,-162.4,-129.6,-43.6,-128.4,-32.8,-126,-151.6,-122.4,-66.4,-117.6,6.8,-117.6,63.1,-112.8,-149.2,-109.2,-86.8,-109.2,-73.6,-105.6,54.8,-104.4,10.3,-100.8,-92.8,-99.6,-146.8,-92.4,-72.4,-92.4,50,-91.2,-103.6,-91.2,10.3,-87.6,-140.8,-82.8,18.8,-81.6,-131.2,-79.2,-108.4,-79.2,50,-78,-65.2,-75.6,-119.2,-72,20,-72,59.5,-68.4,-68.8,-66,-119.2,-64.8,99.1,-61.2,-103.6,-60,58.3,-60,88.3,-57.6,11.5,-56.4,-68.8,-55.2,-90.4,-55.2,-5.1,-52.8,-116.8,-52.8,26,-51.6,51.1,-51.6,71.5,-51.6,86,-50.4,110,-49.2,41.5,-44.4,6.8,-44.4,119.5,-43.1,-62.8,-42,-94,-42,-83.2,-42,53.5,-39.6,128,-38.4,-116.8,-34.8,65.5,-33.6,134,-32.4,-74.8,-32.4,-58,-32.4,10.3,-30,-7.6,-25.1,-116.8,-25.1,76.3,-22.8,2,-22.8,125.5,-22.8,136.3,-21.6,-64,-18,-49.6,-18,-10,-16.8,-23.1,-13.1,-110.8,-13.1,-4,-13.1,82.3,-12,-71.2,-10.8,122,-9.6,-36.4,-8.4,69.1,-4.8,-60.4,-2.4,-5.1,0,-106,0,57.1,0,78.8,2.3,-73.6,2.3,70.3,4.8,-49.6,4.8,120.8,6,8,6,86,7.1,64.3,7.1,100.3,8.3,101.5,12,-103.6,15.5,-34,15.5,65.5,15.5,114.8,18,106.3,19.1,-59.2,19.1,38,19.1,51.1,20.3,5.5,20.3,99.1,21.5,60.8,24,-110.8,26.3,-36.4,26.3,-5.1,27.5,-61.6,27.5,-20.8,28.8,119.5,31.1,92,31.1,128,33.5,9.1,33.5,38,34.8,-42.4,34.8,51.1,36,-42.4,37.1,-53.2,40.8,10.3,40.8,93.1,42,-18.4,42,24.8,43.1,136.3,45.5,56,46.8,-32.8,48,45.1,49.1,11.5,50.3,-56.8,51.5,-114.4,51.5,33.1,51.5,70.3,52.8,47.5,52.8,57.1,52.8,88.3,54,134,56.3,150.8,56.3,167.5,57.5,76.3,58.8,-70,61.1,23.5,62.3,8,62.3,83.5,63.5,141.2,64.8,-119.2,64.8,-43.6,64.8,-14.8,66,-59.2,66,-28,66,41.5,66,161.5,68.3,149.5,69.5,122,70.8,0.8,70.8,95.5,73.1,-74.8,73.1,134,74.3,-127.6,75.5,111.2,76.8,-137.2,79.1,-150.4,79.1,-19.6,80.3,41.5,82.8,95.5,82.8,131.5,84,11.5,85.1,-85.6,86.3,216.7,90,92,91.1,-146.8,91.1,126.8,92.3,45.1,92.3,222.7,93.5,-23.1,93.5,195.2,93.5,224,97.1,-64,98.3,-35.2,98.3,51.1,98.3,64.3,99.5,77.5,99.5,188,100.8,-92.8,100.8,-48.4,103.2,-150.4,103.2,92,104.3,-107.2,104.3,122,105.5,-76,108,24.8,108,105.2,111.5,3.1,112.8,-24.4,112.8,119.5,114,-155.2,114,-103.6,114,-25.6,114,42.8,116.3,-82,116.3,-14.8,117.5,-200.8,117.5,-193.6,118.8,-7.6,118.8,10.3,120,-48.4,120,96.8,122.3,-214,122.3,-17.1,122.3,41.5,123.5,-163.6,124.8,83.5,126,-77.2,127.2,-94,128.3,-107.2,129.5,105.2,130.8,-191.2,130.8,-8.8,130.8,44,130.8,72.8,130.8,111.2,133.2,-174.4,133.2,-13.6,134.3,-217.6,134.3,-54.4,135.5,-82,136.8,-67.6,136.8,64.3,139.2,-115.6,139.2,105.2,140.3,-59.2,140.3,-1.6,142.8,-101.2,142.8,46.3,145.2,-178,145.2,59.5,146.3,-221.2,146.3,-71.2,146.3,-16,146.3,10.3,147.5,96.8,148.8,-92.8,148.8,-50.8,150,24.8,152.3,69.1,153.5,-226,153.5,-185.2,153.5,-100,153.5,35.5,154.8,-113.2,154.8,-22,156,-238,156,82.3,157.2,-82,159.5,-175.6,159.5,-29.2,160.7,28.3,160.7,117.2,162,-269.2,162,-106,162,-11.1,163.2,-251.2,163.2,-84.4,163.2,107.5,164.3,70.3,165.5,0.8,166.7,-262,166.7,126.8,168,100.3,169.2,-38.8,171.5,-94,171.5,-76,171.5,-68.8,171.5,58.3,172.7,12.8,172.7,32,174,-114.4,174,155.5,175.2,-268,175.2,-49.6,175.2,93.1,175.2,114.8,175.2,126.8,176.3,-277.6,177.5,141.2,178.7,47.5,178.7,98,180,-260.8,180,-71.2,180,107.5,180,153.2,181.2,-162.4,181.2,-155.2,181.2,-86.8,181.2,38,182.3,-42.4,183.5,-140.8,184.7,-182.8,184.7,-119.2,187.2,-253.6,190.7,-280,192,-78.4,193.2,-167.2,193.2,-139.6,194.3,-242.8,194.3,-192.4,194.3,-178,196.7,-124,199.2,-280,199.2,101.5,202.7,104,204,-197.2,205.2,-119.2,205.2,-83.2,206.4,-205.6,207.5,-244,211.2,-274,211.2,-115.6,212.4,-217.6,213.6,-155.2,213.6,99.1,214.7,-102.4,216,-230.8,216,-91.6,217.1,-242.8,220.7,-264.4,225.6,-151.6,225.6,-96.4,226.7,-251.2,228,-106,230.4,-238,232.7,-144.4,235.1,-226,238.7,-100,241.1,-139.6,242.4,-215.2,246,-132.4,253.1,-214,253.1,-114.4,256.7,-133.6,262.7,-204.4,266.4,-130,266.4,-119.2,270,-188.8,272.4,-144.4,273.6,-127.6,279.6,-188.8,279.6,-152.8,291.6,-194.8,292.7,-158.8,297.6,-173.2,298.7,-186.4]);
			for (var i:uint = 0; i < points.length/2; i++)
			{
				goalPos.push(new Num3D(points[2 * i], points[2 * i + 1],0));
			}
			goalNum = goalPos.length;
		}
		public override function update(items:Array,num:uint):void 
		{
			_cur++;
			if (_cur < 10)
			{
				if (_pre) preMove(items, num, 1000);				
				return;
			}
			if (_cur<11)
			{
				for (var i:uint = 0; i < num; i++)
				{
					items[i].speed = new Num3D();
					items[i].opos = new Num3D();
					items[i].acceleration = new Num3D();
					items[i].damping = .95;					
				}
			}
			if (_cur < 20) return;
			if (_cur < 21+goalNum/_speed) 
			{
				addSpeed(items,(_cur-20)*_speed);
			}
			if (_cur >= 30  ) 
			{
				var kmax:uint = (_cur - 30) * _speed ;
				var addk:uint = _speed;
				if (kmax >goalNum)
				{
					addk =  goalNum + _speed-kmax;
					kmax = goalNum;
				}
				var tspeed:Num3D;
				var w:Number;
				var h:Number;
				if(kmax<goalNum)
					for (var k:uint = 0; k < addk; k++)
					{
						items[(_cur-30)*_speed+k].opos = items[(_cur-30)*_speed+k].pos.clone();
					}
				for (k=0; k < kmax; k++)
				{
					tspeed = items[k].opos.subtract(items[k].pos);
					items[k].damping = .7;
					tspeed.scaleBy(.1);
					items[k].acceleration = tspeed;
				}
			}
		}
		
		public function addSpeed(items:Array,pcur:uint):void 
		{
			var left:uint = pcur + _speed;
			left = left > goalNum?goalNum:left;
			for (var i:uint = pcur; i < left;i++ )
			{
				var damping:Number = items[i].damping;
				damping = (1 - damping) / (1 - Math.pow(damping, 10));
				var tvc:Num3D = goalPos[i].subtract(items[i].pos);
				tvc.scaleBy(damping);
				items[i].speed = tvc;
			}
		}
		public function preMove(items:Array,num:uint,distance:Number):void 
		{
			var tvc:Num3D;
			var damping:Number;
			damping = .95;
			damping = (1 - damping) / (1 - Math.pow(damping, 10));
			for (var i:uint = goalNum; i < num; i++)
			{
				tvc = MathExtend.pInRound(i, 50, distance*.4+distance*.6*Math.random());
				tvc.decrementBy(items[i].pos);
				tvc.scaleBy(damping);
				items[i].speed = tvc;
			}
			this._pre = false;
		}
	}
	
}